package dfvs

import "time"

const (
	LocalDateTimeFormat = "2006-01-02 15:04:05"

	defaultMQTTServerURL  = "127.0.0.1:1883"
	defaultAlarmLevel     = AlarmLevel1
	defaultAlarmInterval  = time.Minute
	defaultAlarmPrecision = float32(10)

	defaultTopicAlarm       = "DFVS/Channel/Alarm"
	defaultTopicFiberStatus = "DFVS/Channel/Fiber"
	defaultTopicFiberSignal = "DFVS/Channel/Signal"
	defaultTopicHeartbeat   = "DFVS/Heartbeat"
)

var formatTime = func(t string) time.Time {
	at, err := time.Parse(LocalDateTimeFormat, t)
	if err != nil {
		return time.Now()
	}
	return at
}

type Config struct {
	Debug          bool          // 是否调试模式
	MQTTServerURL  string        // MQTT服务地址
	MQTTUsername   string        // MQTT账号
	MQTTPassword   string        // MQTT密码
	AlarmLevel     AlarmLevel    // 报警级别
	AlarmInterval  time.Duration // 报警间隔秒
	AlarmPrecision float32       // 报警精读

	TopicFiberAlarm  string // 订阅的报警消息
	TopicFiberStatus string // 订阅的光纤状态消息
	TopicFiberSignal string // 订阅的光纤状态消息
}

func NewConfig() *Config {
	return &Config{
		Debug:          false,
		MQTTServerURL:  defaultMQTTServerURL,
		MQTTUsername:   "",
		MQTTPassword:   "",
		AlarmLevel:     defaultAlarmLevel,
		AlarmInterval:  defaultAlarmInterval,
		AlarmPrecision: defaultAlarmPrecision,

		TopicFiberAlarm:  defaultTopicAlarm,
		TopicFiberStatus: defaultTopicFiberStatus,
		TopicFiberSignal: defaultTopicFiberSignal,
	}
}

func (c *Config) SetDebug(debug bool) *Config {
	c.Debug = debug
	return c
}

func (c *Config) SetMQTTServerURL(url string) *Config {
	c.MQTTServerURL = url
	return c
}

func (c *Config) SetMQTTUsername(username string) *Config {
	c.MQTTUsername = username
	return c
}

func (c *Config) SetMQTTPassword(password string) *Config {
	c.MQTTServerURL = password
	return c
}

func (c *Config) SetAlarmLevel(level AlarmLevel) *Config {
	c.AlarmLevel = level
	return c
}

func (c *Config) SetAlarmInterval(interval time.Duration) *Config {
	c.AlarmInterval = interval
	return c
}

func (c *Config) SetAlarmPrecision(precision float32) *Config {
	c.AlarmPrecision = precision
	return c
}
